﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace KHOALUAN
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
           Form frm=new frmDanhMucHocKy();
           clsMain.FormHocKy= clsMain.cls.KhoiTaoForm(frm, "HOCKY");

           frm = new frmMonHoc();
           clsMain.FormMonHoc = clsMain.cls.KhoiTaoForm(frm, "MONHOC");
           frm = new frmGridView();
           clsMain.FormGridView = clsMain.cls.KhoiTaoForm(frm, "MONHOC");
           frm = new frmLopHoc();
           clsMain.FormLopHoc = clsMain.cls.KhoiTaoForm(frm, "LOPHOC");
           frm = new frmChucDanh();
           clsMain.FormChucDanh = clsMain.cls.KhoiTaoForm(frm, "CHUCDANH");
           frm = new frmGiaoVien();
           clsMain.FormGiaoVien = clsMain.cls.KhoiTaoForm(frm, "GIAOVIEN");
           frm = new frmKhoa();
           clsMain.FormKhoa= clsMain.cls.KhoiTaoForm(frm, "KHOA");
           frm = new frmCaiDatCTHoc();
           clsMain.FormCaiDatCTHoc = clsMain.cls.KhoiTaoForm(frm, "CHUONGTRINHHOC");
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            clsMain.cls.CreateXMLTable("LOPHOC", clsMain.cls.clsForm.xmlD[clsMain.FormLopHoc]);
            
        }
        private void AddDataToTreeView()
        {
            try
            {
                string strMainMenu = "";
                XElement xelement = XElement.Load("..\\..\\DATA\\" + "MainMenu" + ".xml");
                IEnumerable<XElement> Menu = xelement.Elements();
                
                                 

                // Read the entire XML
                //int j = 0;
                System.Data.DataTable dt = new System.Data.DataTable();
                foreach (XElement col in Menu)
                {
                    strMainMenu = col.Element("Name").Value.ToString();
                    int j = 0;
                    System.Windows.Forms.TreeNode[] nodeChilds = new TreeNode[col.Elements("MenuChilds").Count()];
                    foreach (XElement colChild in col.Elements("MenuChilds"))
                    {
                        TreeNode nodeChild = new TreeNode(colChild.Element("Name").Value.ToString());
                        nodeChild.Tag = colChild.Element("FormID").Value.ToString();
                        nodeChild.ImageIndex = Convert.ToInt32(colChild.Element("ImageIndex").Value);
                        nodeChilds[j] = nodeChild;
                        j++;
                    }
                      
                        TreeNode newNode = new TreeNode(strMainMenu, nodeChilds);
                        trvMainMenu.Nodes.Add(newNode);
                }
                
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("AddDataToTreeView... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            AddDataToTreeView();
        }

        private void trvMainMenu_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (trvMainMenu.SelectedNode.Tag != null)
                {
                    clsMain.cls.CallForm(Convert.ToInt32(trvMainMenu.SelectedNode.Tag), trvMainMenu.SelectedNode.Text);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("trvMainMenu_DoubleClick... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
        }
    }
}
